package com.serviceImpl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;
import javax.servlet.http.Cookie;

import org.springframework.stereotype.Service;
import com.dao.RoleMapper;
import com.dao.UserMapper;
import com.pojo.Permission;
import com.pojo.Role;
import com.pojo.User;
import com.service.*;
import com.util.EndecryptUtils;
import com.util.Updateself;

@Service("userService")
public class UserServiceImpl implements IUserService {

	// 头像的默认值
	private static final String IMG = "images/ali.gif";
	@Resource
	private UserMapper usermapper;
	@Resource
	private RoleMapper rolemapper;

	public User createUser(User user) {
		user.setImg(IMG);
		user = EndecryptUtils.md5Password(user);
		usermapper.adduser(user);
		return user;
	}

	public void changePassword(int userId, String newPassword) {
		User u = usermapper.getUserByid(userId);
		u.setPassword(newPassword);
		usermapper.updateuser(u);
	}

	public void correlationRoles(int userId, int... roleIds) {
		for (int roleid : roleIds) {
			usermapper.adduserrole(userId, roleid);
		}
	}

	public void uncorrelationRoles(int userId, int... roleIds) {
		for (int roleid : roleIds) {
			usermapper.deleteuserrole(userId, roleid);
		}
	}

	public User findByUsername(String username) {
		User u = usermapper.getUserByusername(username);
		return u;
	}

	public Set<String> findRoles(String username) {
		Set<String> roles = new HashSet<String>();
		User u = usermapper.getUserByusername(username);
		for (Role role : u.getRole()) {
			roles.add(role.getName());
		}
		return roles;
	}

	public Set<String> findPermissions(String username) {
		Set<String> permissions = new HashSet<String>();
		User u = usermapper.getUserByusername(username);
		if (u != null) {
			Set<Role> roles = u.getRole();
			if (roles.size() != 0)
				for (Role role : roles) {
					Set<Permission> permiss = rolemapper.getRolebyid(role.getId()).getPermission();
					if (permiss.size() != 0)
						for (Permission permission : permiss) {
							String permissName = permission.getName();
							permissions.add(permissName);
						}
				}
		}

		return permissions;
	}

	public List<User> getallusers(int page, int groups) {
		return usermapper.getusers((page - 1) * groups, groups);
	}

	public User updateuser(User u) {
		usermapper.updateuser(u);
		return u;
	}

	public User updatepwd(User u) {
		usermapper.updatepwd(u);
		return u;
	}

	public void updateself(Updateself updateself) {
		usermapper.updateself(updateself);
	}

	public void deleteuser(int userid) {
		usermapper.deleteuser(userid);
	}

	public User getUser(int uid) {
		User u = usermapper.getUserByid(uid);
		return u;
	}

	public void deleteuserroles(int uid) {
		usermapper.deleteuseroles(uid);
	}

	public int getCounts() {
		return usermapper.getCount();
	}

	public String getCookie(Cookie[] cookies, String key) {
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals(key))
					return cookie.getValue();
			}
		}
		return null;
	}
}
